﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;

namespace Web.admin.CompetitionManage
{
    public partial class ListInner : System.Web.UI.Page
    {
        private AcademyService academyService = new AcademyService();
        private CompetitionService competitionService = new CompetitionService();
        private TeacherService teacherService = new TeacherService();

        const int pagesize = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RP_Bind();
                AcademyList.DataSource = academyService.getAllAcademies();
                AcademyList.DataTextField = "ACA_NAME";
                AcademyList.DataValueField = "ACA_ID";
                AcademyList.DataBind();
                if (Session["selectValue"] != null && Session["selectValue"].ToString() != "")
                    AcademyList.SelectedValue = Session["selectValue"].ToString();
            }
        }

        /// <summary>
        /// reapeater控件绑定
        /// </summary>
        protected void RP_Bind()
        {
            string userFilter = " and 1=1 ";
            if (HttpContext.Current.User != null &&
                HttpContext.Current.User.Identity.IsAuthenticated &&
                HttpContext.Current.User is SitePrincipal)
            {
                SitePrincipal user = HttpContext.Current.User as SitePrincipal;
                SiteIdentity si = user.Identity as SiteIdentity;
                if (si.UserType != 0)
                    return;

                BoTeachers teacher = teacherService.GetTeacher(si.UserID);
                if (teacher.TEA_MANAGER == 1)
                    userFilter = " and COM_MANAGER='" + si.UserID + "' ";
            }

            Pager1.PageSize = pagesize;
            int records = 0;
            if (Session["innerConCom"] != null && Session["innerConCom"].ToString() != "")
            {
                Session["innerConCom"] += " and COM_TYPE=0 " + userFilter;
                RpList.DataSource = competitionService.getPageCompetitions(Session["innerConCom"].ToString(), " COM_ID DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            }
            else
                RpList.DataSource = competitionService.getPageCompetitions(" COM_TYPE=0 " + userFilter, " COM_ID DESC", Pager1.PageSize, Pager1.PageIndex, out records);
            RpList.DataBind();
            //初始化分页条,设置样式
            Pager1.SetPage(records);
            Pager1.ShowPageJump = true;
            Pager1.ShowPageLan = 0; //导航条语言:0=默认，1=中文，2=英语

        }

        /// <summary>
        /// 下拉列表改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AcademyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selAcamediyId = AcademyList.SelectedValue;
            if (selAcamediyId == "")
            {
                Session["selectValue"] = "";
                Session["innerConCom"] = "";
            }
            else
            {
                Session["selectValue"] = selAcamediyId;
                Session["innerConCom"] = "ACA_ID='" + selAcamediyId + "'";
            }
            RP_Bind();
        }
        //批量删除
        protected void btn_Delete_Click(object sender, EventArgs e)
        {
            int j = 0;
            for (int i = 0; i < RpList.Items.Count; i++)
            {
                string id = ((HiddenField)RpList.Items[i].FindControl("Hf_id")).Value;
                CheckBox cb = (CheckBox)RpList.Items[i].FindControl("CbChoose");
                if (cb.Checked)
                {
                    BoCompetitions pid = competitionService.getCompetition(id);
                    competitionService.DeleteCompetition(pid);
                    j++;
                }
            }
            Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='ListInner.aspx'; </script>");
            return;
        }
    }
}